package com.example.config;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.example.common.api.ApiResult;
import com.example.common.api.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    /**
     * 未登录异常
     */
    @ExceptionHandler(NotLoginException.class)
    public ApiResult<?> handleNotLoginException(NotLoginException e) {
        log.error(e.getMessage(), e);
        return ApiResult.failed(ResultCode.UNAUTHORIZED);
    }

    /**
     * 无此权限异常
     */
    @ExceptionHandler(NotPermissionException.class)
    public ApiResult<?> handleNotPermissionException(NotPermissionException e) {
        log.error(e.getMessage(), e);
        return ApiResult.failed(ResultCode.FORBIDDEN);
    }

    /**
     * 无此角色异常
     */
    @ExceptionHandler(NotRoleException.class)
    public ApiResult<?> handleNotRoleException(NotRoleException e) {
        log.error(e.getMessage(), e);
        return ApiResult.failed(ResultCode.FORBIDDEN, "无此角色权限");
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public ApiResult<?> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ApiResult.failed(ResultCode.ERROR);
    }
} 